---comment
---@param inst ent
local function calcPos(inst)
    local start_x,_,start_z = inst:GetPosition():Get()
    local des_x,des_z
    if inst.ACTION_BH_MOBIUS_SWORD_LEAP then
        des_x = inst.ACTION_BH_MOBIUS_SWORD_LEAP.x
        des_z = inst.ACTION_BH_MOBIUS_SWORD_LEAP.z
    end
    if des_x and des_z then
        local dist = C_BH_MOBIUS:calcDist(start_x,start_z,des_x,des_z,true) 
        local res_x,res_z = C_BH_MOBIUS:findPointOnLine(start_x,start_z,des_x,des_z,dist,1)
        inst.Transform:SetPosition(res_x,0,res_z)
    end
end

local timelines = {}

for i = 1,12,2 do
    table.insert(timelines,TimeEvent(i * FRAMES, function(inst)
        calcPos(inst)
    end))
end

table.insert(timelines,TimeEvent(13 * FRAMES, function(inst)
    inst:PerformBufferedAction()
end))

AddStategraphState('wilson',
State{
    name = "bh_mobius_sword_leap",
    tags = { "aoe", "doing", "busy", "nointerrupt", "nopredict", "nomorph" },

    ---comment
    ---@param inst ent
    ---@param data any
    onenter = function(inst, data)
        inst.AnimState:PlayAnimation('atk_leap')

        local buffaction = inst:GetBufferedAction()
        if buffaction ~= nil and buffaction.pos ~= nil then
            local x,y,z = buffaction:GetActionPoint():Get()
            inst.ACTION_BH_MOBIUS_SWORD_LEAP = {x=x,y=y,z=z}
        end
    end,

    onupdate = function(inst)
    end,

    timeline = timelines,

    events =
    {
        EventHandler("animover", function(inst)
            if inst.AnimState:AnimDone() then
                inst.sg:GoToState("idle")
            end
        end),
    },

    onexit = function(inst)
    end,
})


AddStategraphState('wilson_client',
State{
    name = "bh_mobius_sword_leap",
    tags = { "aoe", "doing", "busy", "nointerrupt", "nopredict", "nomorph" },

    ---comment
    ---@param inst ent
    ---@param data any
    onenter = function(inst, data)
        inst:PerformPreviewBufferedAction()
    end,

    timeline =
    {
    },

    onexit = function(inst)
    end,
})